package com.baige.common.utils;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wulg
 * @date 2018/12/12.
 */
public class GeneratorUtils
{

    private static final String TINYINT="tinyint";
    /**
     * 自动生成代码类
     * @param dbUserName 数据库用户名
     * @param dbPassword 数据库密码
     * @param dbUrl 数据库连接
     * @param tableName 表名
     * @param parentName 包名
     * @param entityOutFile entity生成目录
     * @param mapperOutFile mapper生成目录
     * @param mapperXmlOutFile mapperXml生成目录
     * @param serviceOutFile service生成目录
     * @param serviceImplOutFile serviceImp生成目录
     */
    public static void generator(String dbUserName,String dbPassword,String dbUrl,String tableName,String parentName,final String entityOutFile,final String mapperOutFile,final String mapperXmlOutFile,final String serviceOutFile,final String serviceImplOutFile)
    {
        AutoGenerator mpg = new AutoGenerator();

        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir("");
        gc.setFileOverride(true);
        gc.setActiveRecord(true);
        gc.setEnableCache(false);
        gc.setBaseResultMap(true);
        gc.setBaseColumnList(false);
        gc.setAuthor("wulg");
        gc.setMapperName("%sMapper");
        gc.setXmlName("%sMapper");
        gc.setServiceName("%sService");
        gc.setServiceImplName("%sServiceImpl");
        mpg.setGlobalConfig(gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert(){
            // 自定义数据库表字段类型转换【可选】
            @Override
            public DbColumnType processTypeConvert(String fieldType) {
                if(TINYINT.equals(fieldType))
                {
                    return DbColumnType.BASE_INT;
                }
                return super.processTypeConvert(fieldType);
            }
        });
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(dbUserName);
        dsc.setPassword(dbPassword);
        dsc.setUrl(dbUrl);
        mpg.setDataSource(dsc);

        StrategyConfig strategy = new StrategyConfig();
        strategy.setCapitalMode(true);
        strategy.setNaming(NamingStrategy.underline_to_camel);
        // 需要生成的表;
        strategy.setInclude(tableName);
        strategy.setEntityLombokModel(true);
        mpg.setStrategy(strategy);

        PackageConfig pc = new PackageConfig();
        pc.setParent(parentName);
        pc.setEntity("entity");
        pc.setMapper("dao");
        pc.setService("service");
        pc.setXml("dao.mapper");
        pc.setServiceImpl("service.impl");
        mpg.setPackageInfo(pc);

        // 注入自定义配置，可以在 VM 中使用 cfg.abc 【可无】
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>(16);
                this.setMap(map);
            }
        };

        // 自定义 xxList.jsp 生成
        List<FileOutConfig> focList = new ArrayList<>();

        focList.add(new FileOutConfig("/template/entity.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return entityOutFile + tableInfo.getEntityName() + ".java";
            }
        });
        focList.add(new FileOutConfig("/template/mapper.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return mapperOutFile + tableInfo.getMapperName() + ".java";
            }
        });
        focList.add(new FileOutConfig("/template/mapper.xml.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return mapperXmlOutFile + tableInfo.getXmlName() + ".xml";
            }
        });
        focList.add(new FileOutConfig("/template/service.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return serviceOutFile+ tableInfo.getServiceName() + ".java";
            }
        });
        focList.add(new FileOutConfig("/template/serviceImpl.java.vm") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                return serviceImplOutFile + tableInfo.getServiceImplName() + ".java";
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        TemplateConfig tc = new TemplateConfig();
        tc.setEntity("/template/entity.java.vm");
        tc.setMapper("/template/mapper.java.vm");
        tc.setXml("/template/mapper.xml.vm");
        tc.setService("/template/service.java.vm");
        tc.setServiceImpl("/template/serviceImpl.java.vm");
        mpg.setTemplate(tc);

        mpg.execute();
    }


}
